﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GetColliderSurfacePoint : MonoBehaviour
{
    public new MeshCollider collider;
    public Transform originT;
    public Vector3 prePos;
    public float distOs;
    private void Start()
    {
        prePos = originT.position;
    }
    void Update()
    {

        var currTipPos = originT.position;
        if (currTipPos == prePos) return;
        var dir = (currTipPos - prePos).normalized;
        var mask = 1 << LayerMask.NameToLayer("Paintable");
        var dist = Vector3.Distance(prePos, currTipPos);
        Ray ray = new Ray(prePos - dir * dist, dir);
        RaycastHit hit;
        bool bHit = Physics.Raycast(ray, out hit, dist + distOs, mask, QueryTriggerInteraction.Collide);
        if (bHit)
        {
            originT.position = hit.point;
        }
        prePos = originT.position;
    }
}
